<?php 




// add jquery-ui-datepicker for events

function datepicker_init() {
	wp_enqueue_script('jquery');
	wp_enqueue_script('jquery-ui-core');
	wp_enqueue_script('jquery-ui-datepicker');
	wp_enqueue_style('jquery-style', 'http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css');
}

function datepicker_footer() {
	?>

	<script type="text/javascript">
	jQuery(document).ready(function(){
		jQuery("#datepicker").datepicker({
			dateFormat: 'mm-dd-yy'		
		});	
	});	
	</script>

	<?php
}

add_action('admin_init', 'datepicker_init');
add_action('admin_footer', 'datepicker_footer');



add_theme_support( 'custom-header' , array(
  'flex-width'	=> true,
  'height' 	=> 120,
  'uploads' 	=> true

) );

// necessary for theme customization
require_once('theme-options.php'); 


function custom_college_header()
{
add_theme_support( 'custom-header' , array(
  'flex-width' 	=> true,
  'height' 	=> 120,
  'uploads' 	=> true

) );
}

add_action('init', 'custom_college_header');

// custom type event

if (! function_exists( 'create_event_post_type' ) ):
  function create_event_post_type() {
    $labels = array(
      'name' => __( 'Event' ),
      'singular_name' => __('Event'),
      'menu_name' => __('Events'),
      'add_new' => __('Add event'),
      'all_items' => __('All events'),
      'add_new_item' => __('Add event'),
      'edit_item' => __('Edit event'),
      'new_item' => __('New event'),
      'view_item' => __('View item'),
      'search_items' => __('Search events'),
      'not_found' => __('No events found'),
      'not_found_in_trash' => __('No events found in trash' ),
      'parent_item_colon' => __('Parent event' )
    );
    
    $args = array(
      'labels' => $labels,
      'public' => true,
      'publicy_queryable' => true,
      'show_in_nav_menus' => true,
      'query_var' => true,
      'rewrite' => array( 'slug' => 'event'),
      'capability_type' => 'post',
      'hierarchical' => false,
      'supports' => array('title'),
      'menu_position' => 5,
      'register_meta_box_cb' => 'add_event_post_type_metabox'
    );
    
    register_post_type( 'event', $args );
    register_taxonomy( 'custom_category', 'event' );  
  }
  
  add_action( 'init', 'create_event_post_type' );
 endif;


function add_event_post_type_metabox() {
    add_meta_box('event_metabox', 'Event Data', 'event_metabox', 'event', 'normal' );
}

function event_metabox() {
  global $post;
  $custom 	= get_post_custom($post->ID);
  $time  	= unixtime_to_date($custom['event_time'][0]);
  $location 	= $custom['event_location'][0];
  $description 	= $custom['event_description'][0];
  
  ?>
  <div class="event">
    <p><label>Date<br/><input type="text" id="datepicker" name="time" size="50"
	value="<?php echo $time; ?>"></label>
    </p>
    <p><label>Location<br/><input type="text" name="location" size="50"
	value="<?php echo $location; ?>"></label>
    </p>
    <p><label>Description</br><textarea name="description" style="margin: 1px; width: 410px; height: 160px;"><?php echo $description; ?></textarea></label>
    </p> 
  </div>
  
  
<?php }


function date_to_unixtime($date) {
  list($month, $day, $year) = split('-', $date);
  $timeStamp = mktime(0, 0, 0, $month, $day, $year);
  return $timeStamp;
}


function unixtime_to_date($unixtime) {
  if($unixtime):
    return  gmdate("m-d-Y", $unixtime);
  endif;
}


function sort_events_by_metadate($query)
{
  
	$query->set('meta_key', 'event_time');
	$query->set('orderby' , 'meta_value');
	$query->set('order' , 'ASC');
    
}

//add_action('pre_get_posts', 'sort_events_by_metadate');

function event_post_save_meta( $post_id, $post ) {
  // is the user allowed to edit the post or page?
  if (!current_user_can('edit_post', $post->ID)) {
      return $post->ID;
  }
  
  $event_post_meta['event_time'] = date_to_unixtime($_POST['time']);
  $event_post_meta['event_location'] = $_POST['location'];
  $event_post_meta['event_description'] = $_POST['description'];
  
  // add values as custom fields
  
  foreach($event_post_meta as $key => $value) {
    if (get_post_meta( $post->ID, $key, FALSE)) {
      // if the custom field already has a value
      update_post_meta($post->ID, $key, $value);
    } else {
      // if the custom field doesn't have a value
      add_post_meta( $post->ID, $key, $value );
    }
    if (!$value) {
      // delete if blank
      delete_post_meta($post->ID, $key);
    }
    
  }
}

// save the custom fields
add_action( 'save_post', 'event_post_save_meta', 1, 2);


?>
